/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cube.charts.cubedesign;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;

/**
 *
 * @author admin
 */
public class BW implements Designable{
    
    private Graphics g;
    private double p[][];
    private double c[] = new double[9];
    private int x=1;
    private int y=2;
    private int z=3;
    private int w = 200;
    
    
    
    public void paint(){
        // Perspektive: *1+z/1000
        for (int i=1;i<9;i++) {
            c[i] = 1+p[i][z]/1000;
        }

        // 6 Würfelflächen zeichnen
        drawPolygon(1,2,3,4);
        drawPolygon(7,6,5,8);
        drawPolygon(6,2,1,5);
        drawPolygon(4,3,7,8);
        drawPolygon(3,2,6,7);
        drawPolygon(5,1,4,8);
    }
    
    public void drawPolygon(int i, int j, int k, int h) {

        // Kreuzprodukt der eine Fläche aufspannenden Vektoren bilden
        // Wenn Betrag der z-Koordinate positiv: Fläche anzeigen

        if((p[i][x]*c[i]-p[j][x]*c[j])*(p[k][y]*c[k]-p[j][y]*c[j])
          -(p[i][y]*c[i]-p[j][y]*c[j])*(p[k][x]*c[k]-p[j][x]*c[j]) > 0) {
            // |j->i x j->k| > 0

            // x,y-Koordinaten
            int xCoords[] = {(int)(p[i][x]*c[i])+w,(int)(p[j][x]*c[j])+w,
                             (int)(p[k][x]*c[k])+w,(int)(p[h][x]*c[h])+w};
            int yCoords[] = {(int)(p[i][y]*c[i])+w,(int)(p[j][y]*c[j])+w,
                             (int)(p[k][y]*c[k])+w,(int)(p[h][y]*c[h])+w};

            // Winkel zwischen dem Vektor (v1,v2,v3), der senkrecht auf der Fläche steht
            // und dem Einheitsvektor in Richtung der Beleuchtungsquelle (0,0,1)
            // (d.h. Licht von vorne) bestimmt die Helligkeit der Fläche.

            double v1 = (p[i][y]-p[j][y])*(p[k][z]-p[j][z])
                       -(p[i][z]-p[j][z])*(p[k][y]-p[j][y]);
            double v2 = (p[i][z]-p[j][z])*(p[k][x]-p[j][x])
                       -(p[i][x]-p[j][x])*(p[k][z]-p[j][z]);
            double v3 = (p[i][x]-p[j][x])*(p[k][y]-p[j][y])
                       -(p[i][y]-p[j][y])*(p[k][x]-p[j][x]);

            int deg = (int) Math.toDegrees(Math.asin(Math.sqrt(
                        (v2*v2+v1*v1)/(v1*v1+v2*v2+v3*v3))));

            g.setColor(new Color(220-deg,220-deg,220-deg));
            g.fillPolygon(new Polygon(xCoords,yCoords,4));
        }
    }

    public void setG(Graphics g) {
        this.g = g;
    }

    public void setP(double[][] p) {
        this.p = p;
    }

}
